package com.wang.controller.api;

import com.wang.dto.UserDto;
import com.wang.entity.User;
import com.wang.service.UserService;
import com.wang.utils.ConstantUtil;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Classname ApiUserController
 * @Description TODO
 * @Date 2024/8/29 11:50
 * @Created by pc
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
@Api(value = "前台用户接口",tags = "ApiUserController")
public class ApiUserController {
    @Resource
    private UserService userService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    // 前台登录
    @PostMapping("/login")
    @ApiOperation(value = "前台登录",notes = "前台登录，需要提供用户名和密码")
    @ApiImplicitParam(name = "user",value = "用户实体类",required = true,dataType = "User")
    public ResponseUtil login(@RequestBody User user, HttpSession session){
        ResponseUtil responseUtil = userService.apiLogin(user);
        if(responseUtil.getCode().intValue() == ResponseEnum.USER_API_LOGIN_4.getCode()){
            // 登录成功
            session.setAttribute(ConstantUtil.LOGIN_USER_KEY,responseUtil.getData());
        }
        return responseUtil;
    }

    // 获取当前用户
    @GetMapping("/getCurrentUser")
    @ApiOperation(value = "获取当前用户",notes = "前台获取当前登录用户信息")
    public ResponseUtil getCurrentUser(HttpSession session){
        Object loginUser = session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
        return ResponseUtil.get(ResponseEnum.USER_API_GET_CURRENT_INFO,loginUser);
    }

    // 注销
    @GetMapping("/logout")
    @ApiOperation(value = "注销当前用户",notes = "前台注销当前登录用户信息")
    public ResponseUtil logout(HttpSession session){
        // session失效
        session.invalidate();
        return ResponseUtil.get(ResponseEnum.USER_API_LOGOUT_SUCCESS);
    }

    // 注册
    @PostMapping("/reg")
    @ApiOperation(value = "注册账号",notes = "前台用户注册功能")
    @ApiImplicitParam(name = "userDto",value = "用户dto类",required = true,dataType = "UserDto")
    public ResponseUtil reg(@RequestBody UserDto userDto){
        String phoneAndCode = stringRedisTemplate.opsForValue().get(userDto.getUuid());
        ResponseUtil r = null;
        if(phoneAndCode == null){
            // redis中不存在该用户的验证码信息
            r= ResponseUtil.get(ResponseEnum.SMS_VERIFY_4_CODE_NOT_IN_REDIS);
        }else{
            // 验证码存在
            // 比对验证码
            String phoneRedis = phoneAndCode.split("-")[0];
            String codeRedis = phoneAndCode.split("-")[1];
            if(userDto.getPhone().equals(phoneRedis) && userDto.getCode().equals(codeRedis)){
                // redis中存的验证码 和用户提交的是匹配的
                // 完成注册功能
                r = userService.apiAddUser(userDto);
                // 清除redis中的验证码
                stringRedisTemplate.delete(userDto.getUuid());
            }else{
                r = ResponseUtil.get(ResponseEnum.SMS_VERIFY_4_CODE_NOT_MATCH);
            }
        }
        return r;
    }

}
